import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.signature.SignatureMethod;

import com.google.gson.Gson;


public class Main {

    public static void main(String[] args) throws Exception {

        OAuthConsumer consumer = new DefaultOAuthConsumer(
                "396370e2bd0f4c00c490340a9408208404d6e5f22",
                "8583bda88ac89fb804ff1aac1bb80380",
                SignatureMethod.HMAC_SHA1);

        OAuthProvider provider = new DefaultOAuthProvider(consumer,
                "http://api.qa.kinobaza.tv/auth/request-token",
                "http://api.qa.kinobaza.tv/auth/access-token",
                "http://api.qa.kinobaza.tv/auth/authorize");

        System.out.println("Fetching request token from Kinobaza.tv...");

        // we do not support callbacks, thus pass OOB
        String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

        System.out.println("Request token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        System.out.println("Now visit:\n" + authUrl
                + "\n... and grant this app authorization");
        System.out.println("Hit ENTER when you're done:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String pin = br.readLine();

        System.out.println("Fetching access token from Kinobaza...");

        provider.retrieveAccessToken(pin);

        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        
        
        
        
        
        URL url = new URL("http://api.qa.kinobaza.tv/films/361427");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request to Kinobaza...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " " + request.getResponseMessage());
        
        String json = "";
        BufferedReader reader = null;
	    try {
	        reader = new BufferedReader(new InputStreamReader(request.getInputStream() ));
	        for (String line; (line = reader.readLine()) != null;) {
	        	json += line;
	        }
	    } finally {
	        if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
	    }

    
        Film data = new Gson().fromJson(json, Film.class);

        System.out.println( "\n\nСейчас будет информация с Кинобазы:" );
        System.out.println(data.getName());
        System.out.println(data.getOriginalName());
        System.out.println(data.getYear());
    }
}

class Film {
	private Long id;
	private String name;
	private String type;
	private String original_name;
	private Integer year;
	private Integer duration;
	private String best_torrent_quality;
	private String status;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getOriginalName() {
		return original_name;
	}
	public void setOriginalName(String original_name) {
		this.original_name = original_name;
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		this.year = year;
	}
	public Integer getDuration() {
		return duration;
	}
	public void setDuration(Integer duration) {
		this.duration = duration;
	}
	public String getBestTorrentQuality() {
		return best_torrent_quality;
	}
	public void setBestTorrentQuality(String best_torrent_quality) {
		this.best_torrent_quality = best_torrent_quality;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
}
